classdef FileDownloadPacketsGenerator < ClockHandler
    properties
        cbr_packets_generator;
        pareto_x_values;
        pareto_y_values;
    end
    
    methods
        function obj = FileDownloadPacketsGenerator(source_node, dest_node, packet_size_in_Kbytes, data_rate_in_Kbps)
            packet_template = PacketsManager.instance().create_template_packet(source_node, dest_node, packet_size_in_Kbytes);
            obj.cbr_packets_generator = CBRPacketGenerator(packet_template, data_rate_in_Kbps);
            
            params = ParamsSingleton.instance();
            [obj.pareto_x_values, obj.pareto_y_values] = ...
                pareto((params.simulation_time_in_msec / 1000), ...
                       params.file_download_pareto_on_alpha,...
                       params.file_download_pareto_off_alpha,...
                       params.file_download_pareto_on_mu,...
                       params.file_download_pareto_off_mu);
            
            % register on clock tick
            ck = ClockSingleton.instance();
            ck.register_for_clock_ticks(obj);
        end
        
        function clock_tick_handler(obj)
            ck = ClockSingleton.instance();
            time_in_sec = ck.current_time_in_msec / 1000;
            
            params = ParamsSingleton.instance();
            if (time_in_sec <= params.clock_tick_delta_in_msec / 1000)
                obj.cbr_packets_generator.enable();
                return;
            end
            
            should_send_packet = false;
            for i = 2:length(obj.pareto_x_values)
                if (time_in_sec >= obj.pareto_x_values(i-1) && time_in_sec <= obj.pareto_x_values(i))
                    should_send_packet = (obj.pareto_y_values(i) == 1);
                    break;
                end
            end
            
            if should_send_packet
                obj.cbr_packets_generator.enable();
            else
                obj.cbr_packets_generator.disable();
            end
        end
        
    end
    
end